//
//  WNSingleImageBrowser.m
//  Whatnet_iOS
//
//  Created by 北辰 on 2019/6/10.
//  Copyright © 2019 什么网络. All rights reserved.
//

#import "WNSingleImageBrowser.h"

@interface WNSingleImageBrowser ()
@property(nonatomic, strong) UIView *contentView;
@property(nonatomic, strong) UIImageView *imageView;
@property(nonatomic, strong) UIButton *saveBtn;
@property(nonatomic, strong) UIButton *changeBtn;

@property(nonatomic, strong) UIButton *backBtn;

//有些需要缩放动画记住图片的初始位置
@property(nonatomic, assign) CGRect initFrame;

@property(nonatomic, copy) WNSingleImageBrowserHiddenBlock hiddenBlock;
@end

@implementation WNSingleImageBrowser
+ (instancetype)Instance{
    static WNSingleImageBrowser * singleImageBrowser = nil;
    static dispatch_once_t downloadToken;
    dispatch_once(&downloadToken, ^{
        singleImageBrowser = [[WNSingleImageBrowser alloc] init];
    });
    return singleImageBrowser;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, WNScreenWidth, WNScreenHeight);
        self.backgroundColor = [UIColor clearColor];
        [self initUI];
    }
    return self;
}

- (void)initUI{
    self.contentView = [UIView new];
    [self addSubview:self.contentView];
    self.contentView.frame = self.bounds;
    self.contentView.backgroundColor = ThemeColor;
    
    self.imageView = [UIImageView new];
    [self.contentView addSubview:self.imageView];
    self.imageView.frame = CGRectMake(0, 0, self.width, self.width);
    [self.imageView setCenterY:self.contentView.height/2 - 40];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.clipsToBounds = YES;
    
    self.backBtn = [UIButton new];
    [self.contentView addSubview:self.backBtn];
    self.backBtn.frame = self.bounds;
    [self.backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchDown];
    
    self.changeBtn = [UIButton new];
    [self.contentView addSubview:self.changeBtn];
    [self.changeBtn setTitle:@"更换" forState:0];
    [self.changeBtn setTitleColor:WNHexColor(0xffffff) forState:0];
    [self.changeBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    self.changeBtn.backgroundColor = [UIColor clearColor];
    self.changeBtn.layer.cornerRadius = 3;
    self.changeBtn.layer.masksToBounds = YES;
    self.changeBtn.layer.borderColor = WNHexColor(0xffffff).CGColor;
    self.changeBtn.layer.borderWidth = 1;
    [self.changeBtn setSize:CGSizeMake(70, 36)];
    [self.changeBtn setTop:self.imageView.bottom+80];
    [self.changeBtn setCenterX:self.width/2];
    [self.changeBtn addTarget:self action:@selector(changeBtnAction) forControlEvents:UIControlEventTouchDown];
    
    self.saveBtn = [UIButton new];
    [self.contentView addSubview:self.saveBtn];
    [self.saveBtn setImage:[UIImage imageNamed:@"download"] forState:0];
    self.saveBtn.backgroundColor = [UIColor clearColor];
    [self.saveBtn setSize:CGSizeMake(40, 40)];
    [self.saveBtn setBottom:WNScreenHeight - self.safe_bottom - 15];
    [self.saveBtn setRight:WNScreenWidth - 20];
    [self.saveBtn addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchDown];
}

- (void)changeBtnAction{
    if (self.WNChangeImageBlock) {
        self.WNChangeImageBlock();
    }
}

- (void)saveBtnAction{
    if (!self.imageView.image) {
        return;
    }
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)backBtnAction{
    [self hidden];
}

- (void)configData{
    
}

//保存图片
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    NSString * msg = nil;
    if (error == nil) {
        msg = @"保存成功";
    }else{
        msg = @"保存失败";
    }
    [WNTool show:msg];
}

- (void)showWithImage:(UIImage *)image isSelf:(BOOL)isSelf{
    self.changeBtn.hidden = !isSelf;
    self.imageView.image = image;
    self.saveBtn.hidden = NO;
    self.imageView.frame = CGRectMake(0, 0, self.width, self.width);
    [self.imageView setCenterY:self.contentView.height/2 - 40];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:.28 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)showSingImage:(UIImage *)image initFrame:(CGRect)initFrame{
    [self showSingImage:image initFrame:initFrame hiddenDownloadBtn:NO hiddenBlock:nil];
}


- (void)showSingimageName:(NSString *)imageName initFrame:(CGRect)initFrame{
    [self showSingimageName:imageName initFrame:initFrame hiddenDownloadBtn:NO hiddenBlock:nil];
}

//显示单张图片,initFrame动画初始位置
- (void)showSingimageName:(NSString *)imageName initFrame:(CGRect)initFrame hiddenDownloadBtn:(BOOL)hidden hiddenBlock:(WNSingleImageBrowserHiddenBlock)block{
    _hiddenBlock = block;
    _initFrame  = initFrame;
    self.saveBtn.hidden = hidden;
    self.changeBtn.hidden = YES;
    self.imageView.frame = initFrame;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageName]];
    [self configData];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
        self.imageView.frame = self.bounds;
    } completion:^(BOOL finished) {
        
    }];
}





//显示单张图片,initFrame动画初始位置
- (void)showSingImage:(UIImage *)image initFrame:(CGRect)initFrame hiddenDownloadBtn:(BOOL)hidden hiddenBlock:(WNSingleImageBrowserHiddenBlock)block{
    _hiddenBlock = block;
    _initFrame  = initFrame;
    self.saveBtn.hidden = hidden;
    self.changeBtn.hidden = YES;
    self.imageView.frame = initFrame;
    self.imageView.image = image;
    [self configData];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
        self.imageView.frame = self.bounds;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hidden{
    [UIView animateWithDuration:.3 animations:^{
        if (!CGRectEqualToRect(self.initFrame, CGRectZero)) {
            self.imageView.frame = self.initFrame;
        }
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (self.hiddenBlock) {
            self.hiddenBlock();
        }
    }];
}
@end
